// HomeActivity.java
package com.example.sqltowebdemo;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import java.util.List;

public class HomeActivity extends AppCompatActivity {
    private EditText editTextNote;
    private Button buttonAddNote, buttonViewNotes;
    private ApiService apiService;
    private UserDao userDao;
    private String username;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);

        apiService = RetrofitClient.getClient().create(ApiService.class);
        userDao = new UserDao(this);
        username = getIntent().getStringExtra("username");

        editTextNote = findViewById(R.id.editTextNote);
        buttonAddNote = findViewById(R.id.buttonAddNote);
        buttonViewNotes = findViewById(R.id.buttonViewNotes);

        // 同步数据
        syncData();

        buttonAddNote.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String noteText = editTextNote.getText().toString();
                Note note = new Note(username, noteText);
                Call<User> call = apiService.addNote(note);
                call.enqueue(new Callback<User>() {
                    @Override
                    public void onResponse(Call<User> call, Response<User> response) {
                        if (response.isSuccessful()) {
                            Toast.makeText(HomeActivity.this, "Note Added", Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(HomeActivity.this, "Failed to Add Note", Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    public void onFailure(Call<User> call, Throwable t) {
                        Toast.makeText(HomeActivity.this, "Error: " + t.getMessage(), Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });

        buttonViewNotes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(HomeActivity.this, NotesActivity.class);
                intent.putExtra("username", username);
                startActivity(intent);
            }
        });
    }

    private void syncData() {
        Call<List<Note>> call = apiService.getNotes(username);
        call.enqueue(new Callback<List<Note>>() {
            @Override
            public void onResponse(Call<List<Note>> call, Response<List<Note>> response) {
                if (response.isSuccessful()) {
                    List<Note> notes = response.body();
                    long userId = getUserId(username);
                    userDao.syncNotes(notes, userId);
                }
            }

            @Override
            public void onFailure(Call<List<Note>> call, Throwable t) {
                Toast.makeText(HomeActivity.this, "Error: " + t.getMessage(), Toast.LENGTH_SHORT).show();
            }
        });
    }

    private long getUserId(String username) {
        // 这里假设我们已经知道用户ID，实际应用中需要从数据库中查询
        return 1; // 示例用户ID
    }
}